﻿Imports IntoNails.Model
Imports System.IO

Namespace DataAccess

    <Serializable()> _
    Public Class UserRepository
        Inherits RepositoryBase(Of User)
        Implements IUserRepository

#Region "Declarations"
        Private ReadOnly _dataFile As String
        Public ReadOnly Property DataFile() As String
            Get
                Return _dataFile
            End Get
        End Property
#End Region

#Region "Constants"
        Private Const USER_FILE As String = "{0}\Data\Users.dat"
#End Region

#Region "Constructors"

        Public Sub New()
        End Sub

        Public Sub New(ByVal dataFile As String)
            Me.New()
            Me._dataFile = dataFile
            Me.Load()
        End Sub

#End Region

        Public Function Add(ByVal data As User) As Boolean _
            Implements IDataRepository(Of User).Add

            If Not Me.InnerDataList.Contains(data) Then
                Me.InnerDataList.Add(data)
                Return True
            End If

            Return False
        End Function

        Public Function GetItem(ByVal uniqueCode As Integer) As User _
            Implements IDataRepository(Of User).GetItem

            Return Me.InnerDataList.Where(Function(u) u.EmployeeCode = uniqueCode).FirstOrDefault()

        End Function

        Public Function GetRawCollection() As List(Of User) _
            Implements IDataRepository(Of User).GetRawCollection

            Return Me.InnerDataList

        End Function

        Public Function Remove(ByVal uniqueCode As Integer) As Boolean _
            Implements IDataRepository(Of User).Remove

            ' TODO: find a way to block user by employee-code...
            Return False

        End Function

        Public Function Update(ByVal uniqueCode As Integer, ByVal newData As User) As Boolean _
            Implements IDataRepository(Of User).Update

            ' TODO: find a way to update user by employee-code...
            Return False

        End Function

        Public Function Login(ByVal username As String, ByVal password As String) As User _
            Implements IUserRepository.Login

            Return Me.InnerDataList.Where(Function(u) u.Username = username And u.Password = HashUtils.Hash(password)).FirstOrDefault()

        End Function

        Public Function GetUser(ByVal username As String) As User _
            Implements IUserRepository.GetUser

            Return Me.InnerDataList.Where(Function(u) u.Username = username).FirstOrDefault()

        End Function

#Region "RepositoryBase Overriden"

        Public Overrides Sub Load()
            If IO.File.Exists(Me.DataFile) Then
                Using readStream As New FileStream(Me.DataFile, FileMode.Open, FileAccess.Read)
                    Me.InnerDataList = readStream.Deserialize(Of List(Of User))()
                End Using
            Else
                Me.InnerDataList = New List(Of User)()
            End If
        End Sub

        Public Overrides Sub Save()
            Using writeStream As New FileStream(Me.DataFile, FileMode.OpenOrCreate, FileAccess.Write)
                Me.InnerDataList.Serialize(writeStream)
            End Using
        End Sub

#End Region

    End Class

End Namespace